﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CentralOffice.PublicAffair.Data;
using CentralOffice.PublicAffair.BusinessActions;

using FavoriteTable = CentralOffice.PublicAffair.Data.FavoriteDataSet.FavoriteDataTable;
using FavoriteRow = CentralOffice.PublicAffair.Data.FavoriteDataSet.FavoriteRow;




namespace CentralOffice.WebClient.PublicAffairs
{
    public partial class FavoriteEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                string favoriteId = HttpContext.Current.Request.QueryString["id"];
                if (String.IsNullOrEmpty(favoriteId))
                {
                    //txtFiledDate.Text = DateTime.Now.ToString();
                    //txtFiledDate.Enabled = false;

                    //txtOrderNumber.Text = (Archive.BusinessActions.Archive.GetMaxOrderNumber() + 1).ToString();

                    txtLink.Text = "http://";
                }
                else
                {
                    SetFields(favoriteId);
                }
            }

        }


        private void SetFields(string id)
        {
            if (String.IsNullOrEmpty(id))
                return;
            FavoriteRow favorite = Favorite.GetById(new Guid(id));

            txtName.Text = favorite.Name;

            txtLink.Text = favorite.Link;
            txtRemark.Text = favorite.IsRemarkNull() ? "" : favorite.Remark;
        }

        private FavoriteDataSet GetFields(string id)
        {
            FavoriteDataSet favoriteDataSet = null;
            FavoriteRow favorite = null;

            if (String.IsNullOrEmpty(id))
            {
                favoriteDataSet = new FavoriteDataSet();
                favorite = favoriteDataSet.Favorite.NewFavoriteRow();
                SiteUtil.PopulateCommonField(favorite, Guid.NewGuid());
            }
            else
            {
                favorite = Favorite.GetById(new Guid(id));
                favoriteDataSet = favorite.Table.DataSet as FavoriteDataSet;
            }

            favorite.Name = txtName.Text;
            favorite.Link = txtLink.Text;
            favorite.Remark = txtRemark.Text;
            
            

            if (String.IsNullOrEmpty(id))
            {
                favorite.Table.Rows.Add(favorite);
                //document.Table.Rows.Add(document);
            }

            return favoriteDataSet;
        }

       


        private void ClearFields()
        {
            txtName.Text = "";
            txtLink.Text = "http://";
            txtRemark.Text = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Guid id = this.Save();
            ClearFields();
            Response.Redirect("FavoriteQuery.aspx?id=" + id);
        }

        protected void btnContinue_Click(object sender, EventArgs e)
        {
            this.Save();
            ClearFields();
        }

        private Guid Save()
        {
            string id = HttpContext.Current.Request.QueryString["id"];

            FavoriteDataSet favoriteDataSet = GetFields(id);
            Favorite.Update(favoriteDataSet);

            if (String.IsNullOrEmpty(id))
            {
                id = (favoriteDataSet.Favorite.Rows[0] as FavoriteRow).Id.ToString();
            }

            return new Guid(id);
        }
    }
}